import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    cartList:[]
  },
  mutations: {
    addCart(state,item){
      let findItem = state.cartList.find((ite)=>{
        return item.id === ite.id
      })
      if(findItem){
        findItem.cont++
      }else{
        Vue.set(item,"cont",1)
        state.cartList.push(item)
      }
      console.log(state.cartList)
    },
    addItem(state,id){
      let findItem = state.cartList.find((ite)=>{
        return id === ite.id
      })
      findItem.cont++
    },
    subItem(state,id){
      let findItem = state.cartList.find((ite)=>{
        return id === ite.id
      })
      if(findItem.cont>1){
        findItem.cont--
      }else{
        let findIndex = state.cartList.findIndex((ite)=>{
          return id === ite.id
        })
        state.cartList.splice(findIndex,1)
      }
    }
  },
  actions: {
  },
  modules: {
  }
})
